package com.cn.onetrip.untility;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.IOException;

/**
 * @author Tiger Tang
 * @since 110612 Date: 12-5-2
 */
public class SDCardUtils {

	public static String getSDPath(Context context) {
		String strPath = "";
		File sdDir = null;

		boolean sdCardExist = Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();
			strPath = sdDir.toString();
		} else {
			// RW data
			strPath = Environment.getDataDirectory() + "/data/"
					+ context.getPackageName() + "/files";
			File f = new File(strPath);
			if (!f.exists()) {
				f.mkdir();
			}
		}
		return strPath + File.separator;
	}

	public static String getFlashPath(Context context) {
		String strPath = Environment.getDataDirectory() + "/data/"
				+ context.getPackageName() + "/files";
		File f = new File(strPath);
		if (!f.exists()) {
			f.mkdir();
		}
		return strPath + File.separator;
	}

	/**
	 * @throws java.io.IOExceptionstrPath
	 */
	public static File createSDFile(String fileName) throws IOException {
		if (isSDCardAvailible()) {
			File file = new File(fileName);
			if (!file.exists()) {
				File parent = file.getParentFile();
				if (!file.getParentFile().exists()) {
					parent.mkdirs();
				}
				// file.createNewFile();
				file.mkdir();
			}
			return file;
		} else {
			File file = new File(fileName);
			if (!file.exists()) {
				File parent = file.getParentFile();
				if (!file.getParentFile().exists()) {
					parent.mkdirs();
				}
				// file.createNewFile();
				file.mkdir();
			}
			return file;
		}
	}

	public static File createSDFileHiddlen(String fileName) throws IOException {
		if (isSDCardAvailible()) {
			File file = new File(fileName);
			if (!file.exists()) {
				File parent = file.getParentFile();
				if (!file.getParentFile().exists()) {
					parent.mkdirs();
				}
				file.mkdir();
			}
			return file;
		} else {
			File file = new File(fileName);
			if (!file.exists()) {
				File parent = file.getParentFile();
				if (!file.getParentFile().exists()) {
					parent.mkdirs();
				}
				// file.createNewFile();
				file.mkdir();
			}
			return file;
		}
	}

	/**
	 * 
	 */
	public static boolean isSDCardAvailible() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}

	public static int getDirSize(File file) {

		if (file.exists()) {
			if (file.isDirectory()) {
				File[] children = file.listFiles();
				int size = 0;
				for (File f : children)
					size += getDirSize(f);
				return size;
			} else {
				int size = (int) file.length();
				return size;
			}
		} else {
			System.out.println("");
			return 0;
		}
	}

	public static void deleteSDCardFolder(File dir) {
		Log.d("deleteSDCardFolder", "DELETEPREVIOUS TOP" + dir.getPath());
		if (dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				File temp = new File(dir, children[i]);
				if (temp.isDirectory()) {
					Log.d("deleteSDCardFolder",
							"Recursive Call" + temp.getPath());
					deleteSDCardFolder(temp);
				} else {
					Log.d("deleteSDCardFolder", "Delete File" + temp.getPath());
					boolean b = temp.delete();
					if (b == false) {
						Log.d("deleteSDCardFolder", "DELETE FAIL");
					}
				}
			}
			dir.delete();
		}
	}

	public static void deleteSDCardFolderFile(File dir) {
		Log.d("deleteSDCardFolder", "DELETEPREVIOUS TOP" + dir.getPath());
		if (dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				File temp = new File(dir, children[i]);
				if (temp.isDirectory()) {
					Log.d("deleteSDCardFolder",
							"Recursive Call" + temp.getPath());
					deleteSDCardFolderFile(temp);
				} else {
					Log.d("deleteSDCardFolder", "Delete File" + temp.getPath());
					boolean b = temp.delete();
					if (b == false) {
						Log.d("deleteSDCardFolder", "DELETE FAIL");
					}
				}
			}
		}
	}
}
